package com.d3works.apps.hr.db.repo;

import com.d3works.apps.base.db.repo.SequenceRepository;
import com.d3works.apps.base.service.administration.SequenceService;
import com.d3works.apps.hr.db.EmploymentContract;
import com.d3works.exception.D3worksException;
import com.d3works.exception.service.TraceBackService;
import com.google.inject.Inject;

public class EmploymentContractHRRepository extends EmploymentContractRepository {

  @Inject protected SequenceService sequenceService;

  @Override
  public EmploymentContract save(EmploymentContract employmentContract) {
    if (employmentContract.getRef() == null) {
      String seq = null;
      try {
        seq =
            sequenceService.getSequenceNumber(
                SequenceRepository.EMPLOYMENT_CONTRACT,
                employmentContract.getPayCompany(),
                EmploymentContract.class,
                "ref");
      } catch (D3worksException e) {
        TraceBackService.traceExceptionFromSaveMethod(e);
      }
      employmentContract.setRef(seq);
    }

    return super.save(employmentContract);
  }
}
